/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 04.02.2006
 *
 *************************************************/
package fightplan.views.tables;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JTable;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

import fightplan.algorithmen.SeedComparator;
import fightplan.business.AbstractFighter;
import fightplan.business.Category;
import fightplan.ui.IntegerCellEditor;

@SuppressWarnings("serial")
public class SeededTable extends JTable {

	public SeededTable() {
		super(new SeedTableModel());

		setRowHeight(20);
		TableColumnModel cm = getColumnModel();
		cm.getColumn(2).setCellEditor(new IntegerCellEditor());
	}

	public void setCategory(Category category) {
		SeedTableModel seedTableModel = (SeedTableModel) getModel();
		seedTableModel.setCategory(category);
	}

	private static class SeedTableModel extends AbstractTableModel implements ListDataListener {
		private Category category;
		private List<AbstractFighter> sortedFighters = new ArrayList<AbstractFighter>();

		public SeedTableModel() {
		}

		public void setCategory(Category category) {
			if (this.category != null) {
				this.category.getFighters().removeListDataListener(this);
			}
			this.category = category;
			if (category != null) {
				sortedFighters = new ArrayList<AbstractFighter>(category.getFighters());
				Collections.sort(sortedFighters, new SeedComparator(category));
				category.getFighters().addListDataListener(this);
			} else {
				sortedFighters = new ArrayList<AbstractFighter>();
			}
			fireTableDataChanged();
		}

		public int getColumnCount() {
			return 3;
		}

		public int getRowCount() {
			return sortedFighters.size();
		}

		@Override
		public String getColumnName(int columnIndex) {
			if (columnIndex == 0) {
				return "Name";
			} else if (columnIndex == 1) {
				return "Dojo";
			} else if (columnIndex == 2) {
				return "Rang";
			}
			return "";
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			AbstractFighter fighter = sortedFighters.get(rowIndex);
			if (columnIndex == 0) {
				return fighter.getName();
			} else if (columnIndex == 1) {
				return fighter.getDojo() != null ? fighter.getDojo().getName() : "";
			} else if (columnIndex == 2) {
				return calcSeed(fighter);
			}
			return "";
		}

		private int calcSeed(AbstractFighter fighter) {
			int pos = 1;
			for (AbstractFighter seededFighter : category.getSeeds()) {
				if (seededFighter.equals(fighter))
					return pos;
				pos++;
			}
			return 0;
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return columnIndex == 2;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			AbstractFighter fighter = sortedFighters.get(rowIndex);
			int newPos = 0;
			try {
				newPos = Integer.parseInt("" + aValue);
			} catch (Exception x) {
				// silent catch
			}
			category.getSeeds().remove(fighter);
			if (newPos > 0) {
				newPos = newPos - 1; // List count starts at 0
				if (newPos > category.getSeeds().size()) {
					newPos = category.getSeeds().size();
				}
				category.getSeeds().add(newPos, fighter);
			}
			Collections.sort(sortedFighters, new SeedComparator(category));
			fireTableRowsUpdated(0, getRowCount());
		}

		// ListDateListener

		public void intervalAdded(ListDataEvent e) {
			changed();
		}

		public void intervalRemoved(ListDataEvent e) {
			changed();
		}

		public void contentsChanged(ListDataEvent e) {
			changed();
		}

		private void changed() {
			this.sortedFighters = new ArrayList<AbstractFighter>(category.getFighters());
			Collections.sort(sortedFighters);
			fireTableDataChanged();
		}

	}

}
